package com.example.smart_city_0829.activity.sdkm;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.DeathData;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.SmartCity;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.sdkm.Comment;
import com.example.smart_city_0829.entity.sdkm.Kmlb;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.util.stream.Collectors;

public class DetailActivity extends BaseActivity {

    private ImageView im;
    private TextView name;
    private TextView content;
    private MaxSizeLv lv;
    private EditText comment;
    private Kmlb kmlb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initView() {
        im = (ImageView) findViewById(R.id.im);
        name = (TextView) findViewById(R.id.name);
        content = (TextView) findViewById(R.id.content);
        lv = (MaxSizeLv) findViewById(R.id.lv);
        comment = (EditText) findViewById(R.id.comment);
    }

    @Override
    protected void initData() {
        String data = getIntent().getStringExtra("data");
        kmlb = gson.fromJson(data, Kmlb.class);

        Glide.with(this)
                .load(kmlb.getPic())
                .into(im);
        name.setText(kmlb.getName());
        content.setText(kmlb.getContent());

        setCommentLv();
    }

    @Override
    protected void initEvent() {
        comment.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    DeathData.commentList.add(new Comment(kmlb.getId(), comment.getText().toString()));
                    comment.setText("");
                    setCommentLv();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return null;
    }

    private void setCommentLv() {
        lv.setAdapter(new CommonAdapter<Comment, CommonAdapter.CommentListViewHolder>(
                DeathData.commentList.stream().filter(data -> data.getTargetId() == kmlb.getId()).collect(Collectors.toList()),
                DetailActivity.this
        ) {
            @Override
            protected void setData(Comment data, int position, CommentListViewHolder viewHolder, View convertView, Context context) {
                viewHolder.im.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(R.drawable.ic_baseline_person_24)
                        .into(viewHolder.im);

                viewHolder.name.setText("name");
                viewHolder.content.setText(data.getContent());
                viewHolder.cb.setTag("致敬");
            }

            @Override
            protected CommentListViewHolder getViewHolder() {
                return new CommentListViewHolder();
            }

            @Override
            protected void initView(CommentListViewHolder viewHolder, View convertView) {
                viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            }

            @Override
            protected int getItemLayoutResourceId() {
                return R.layout.comment_list_item;
            }
        });
    }
}